#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import time
import requests
from downloadPictures import settings


class ImageDownloadPipeline(object):

    def process_item(self, item, spider):
        if 'image_urls' in item:
            images = []

            dir_path = '%s/%s' % (settings.IMAGE_STORE, item['name'][0])

            if not os.path.exists(dir_path):
                os.makedirs(dir_path)
            for image_url in item['image_urls']:
                name_list = image_url.split('/')[5:]
                name_list.insert(0, str(time.time()))
                image_name = '_'.join(name_list)
                file_path = '%s/%s' % (dir_path, image_name)
                images.append(file_path)
                if os.path.exists(file_path):
                    continue
                print("开始下载图片：" + image_name)
                # 下载图片
                with open(file_path, 'wb') as handle:
                    response = requests.get(
                        image_url, headers={'User-Agent': 'Mozilla/5.0'},
                        stream=True, timeout=15)
                    for block in response.iter_content(1024):
                        if not block:
                            break

                        handle.write(block)
                print("图片" + image_name + "下载完成")
            item['images'] = images
        return item
